package com.zhongx.xingcd.zxxcd.controller;

import com.zhongx.xingcd.zxxcd.bean.Trick;
import com.zhongx.xingcd.zxxcd.common.Isphonenum;
import com.zhongx.xingcd.zxxcd.service.TrickInfoService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class TrickController {
    @Autowired
    private TrickInfoService trickInfoService;

    /**
     * 获取行程单信息
     * @param jsonParam
     * @return
     */
    @RequestMapping(value = "/getTickInfo",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getTickInfo(@RequestBody JSONObject jsonParam, HttpServletResponse response) {
        //System.out.println("接收的数据 "+jsonParam);

        Map map = new HashMap();
        map.put("ticket_number",jsonParam.get("ticket_number"));
        map.put("travel_itinerary_id",jsonParam.get("travel_itinerary_id"));
        map.put("ticket_state",jsonParam.get("ticket_state"));
        map.put("ticket_style",jsonParam.get("ticket_style"));
        map.put("ticket_username",jsonParam.get("ticket_username"));
        List<Trick> list = trickInfoService.getTrickMes(map);


        JSONObject jsonObject = new JSONObject();

            jsonObject.put("result_data_size",list.size());
            jsonObject.put("result_data",list);
            jsonObject.put("result_code",0);
            jsonObject.put("result_mes","行程单信息");

        return jsonObject.toString();
    }

    /**
     * 手动插入行程单
     * @param jsonParam
     * @param response
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/insertTick",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String insertTick(@RequestBody JSONObject jsonParam, HttpServletResponse response,HttpSession httpSession) {
        JSONObject jsonObject = new JSONObject();
        System.out.println("接收的数据 "+jsonParam+httpSession.getAttribute("username"));

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(calendar.getTime()));

        int sa = trickInfoService.getTrickMesNumByName((String) jsonParam.get("ticket_number"));
        if(sa!=0){
            jsonObject.put("result_data",sa);
            jsonObject.put("result_code",2);
            jsonObject.put("result_mes","单号错误");
            return jsonObject.toString();
        }
        Map map = new HashMap();
        map.put("travel_itinerary_id", Isphonenum.getUUID());
        map.put("ticket_number",jsonParam.get("ticket_number"));
        map.put("ticket_money",jsonParam.get("ticket_money"));
        map.put("ticket_style",jsonParam.get("ticket_style"));
        map.put("ticket_allmoney",jsonParam.get("ticket_allmoney"));
        map.put("ticket_state","审核中");
        map.put("ticket_username",httpSession.getAttribute("username"));
        map.put("ticket_inserttime",formatter.format(calendar.getTime()));

        int a = trickInfoService.intoTrickMes(map);
        System.out.println(a);

        if(a!=0){
            jsonObject.put("result_data",a);
            jsonObject.put("result_code",0);
            jsonObject.put("result_mes","插入行程单信息");

            return jsonObject.toString();
        }else {
            jsonObject.put("result_data", a);
            jsonObject.put("result_code", 1);
            jsonObject.put("result_mes", "插入行程单信息错误");

            return jsonObject.toString();
        }
    }

    @RequestMapping(value = "/insertMuchTick",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String insertMuchTick(@RequestBody JSONObject jsonParam, HttpServletResponse response,HttpSession httpSession) {
        String uuida=Isphonenum.getUUID();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(calendar.getTime()));

        JSONObject jsonObject = new JSONObject();
        List tricklist = new ArrayList();
        System.out.println("接收的数据 "+jsonParam+httpSession.getAttribute("username"));
        //获得excel的票号信息
        JSONArray trickmes=jsonParam.getJSONArray("result_data");
        for(int i=0;i<trickmes.size();i++){
           JSONObject jsonObjectqq = (JSONObject) trickmes.get(i);
            String trickname = jsonObjectqq.getString("ticket_number");
            int sa = trickInfoService.getTrickMesNumByName(trickname);
            if(sa!=0){
                jsonObject.put("result_data",sa);
                jsonObject.put("result_code",2);
                jsonObject.put("result_mes","单号错误,已有该单号");
                return jsonObject.toString();
            }
            jsonObjectqq.put("ticket_state","审核中");
            jsonObjectqq.put("ticket_username",httpSession.getAttribute("username"));
            jsonObjectqq.put("travel_itinerary_id", uuida);
            jsonObjectqq.put("ticket_allmoney","1000");
            jsonObjectqq.put("ticket_inserttime",formatter.format(calendar.getTime()));
            tricklist.add(jsonObjectqq);
        }
        System.out.println(tricklist.toString()+"  s");
        int a=trickInfoService.addMuchTrickMes(tricklist);
        System.out.println(a);

        if(a!=0){
            jsonObject.put("result_data",a);
            jsonObject.put("result_code",0);
            jsonObject.put("result_mes","插入行程单信息");

            return jsonObject.toString();
        }else {
            jsonObject.put("result_data", a);
            jsonObject.put("result_code", 1);
            jsonObject.put("result_mes", "插入行程单信息错误");

            return jsonObject.toString();
        }
    }


    /**
     * 订单管理
     */
    @RequestMapping("/goticketManage")
    public String goticketManage(HttpSession session) {
        return "zxxcd/admin/admin_ticket_manage";

    }
}
